package top.maof.book.spider.develop;

import lombok.extern.slf4j.Slf4j;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import top.maof.book.model.Book;
import top.maof.book.model.Contents;
import top.maof.book.service.BookService;
import top.maof.book.service.ContentsService;
import us.codecraft.webmagic.Task;
import us.codecraft.webmagic.pipeline.PageModelPipeline;

/**
 * 保存目录信息
 */
@Component
@Slf4j
public class ContentsPageModelPipeline implements PageModelPipeline<Split> {
    @Autowired
    BookService bookService;

    @Autowired
    ContentsService contentsService;

    @Override
    public void process(Split split, Task task) {
        Book book1 = split.getBook();
        if (book1 != null) {
            Book book = bookService.selectByNameAndAuthor(book1.getName(), book1.getAuthor());
            Contents contents = split.getContents();
            if (book != null && contents != null) {
                contents.setBookId(book.getId());

                // 不完本不保存章节列表
                if (!book.getState().contains("完本")) {
                    contents.setContent(null);
                }
                contentsService.add(contents);
            }
        }

    }
}
